package num103;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author : DuJiabao
 * @Project : sword-finger-offer
 * @Time : 2021/5/9 2:36
 * @Desc : 我放弃了。。。
 */
class Solution {
  public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
    LinkedList<TreeNode> queue = new LinkedList<>();
    ArrayList<List<Integer>> res = new ArrayList<>();
    if (root == null) return res;
    boolean isLeft = false;
    queue.addFirst(root);
    while (!queue.isEmpty()) {
      int size = queue.size();
      ArrayList<Integer> list = new ArrayList<>();
      for (int i = 0; i < size; i++) {
        if (isLeft) {
          TreeNode node = queue.removeLast();
          list.add(node.val);
          queue.addLast(node.left);
          queue.addLast(node.right);
        } else {
          TreeNode node = queue.removeFirst();
          list.add(node.val);
          queue.addFirst(node.left);
          queue.addFirst(node.right);
        }
      }
      res.add(list);
      isLeft = !isLeft;
    }
    return res;
  }

  public static void main(String[] args) {
    TreeNode root = new TreeNode(3);
    root.left = new TreeNode(9);
    root.right = new TreeNode(20);
    root.right.left = new TreeNode(15);
    root.right.right = new TreeNode(7);
    List<List<Integer>> res = new Solution().zigzagLevelOrder(root);
    System.out.println(res);
  }
}
